package com.springboot.astra.controller;

import com.springboot.astra.bean.IdxCarousel;
import com.springboot.astra.bean.User;
import com.springboot.astra.result.DataResult;
import com.springboot.astra.service.IdxCarouselService;
import com.springboot.astra.service.UserService;
import com.springboot.astra.util.JwtUtils;
import com.springboot.astra.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    UserService userService;
    @Autowired
    IdxCarouselService idxCarouselService;

    /**
     * 用户登录
     * @param map
     * @return
     */
    @PostMapping("/userLogin")
    public DataResult userLogin(@RequestParam Map<String,String> map){
        return login(map);
    }

    @PostMapping("/adminLogin")
    public DataResult adminLogin(@RequestParam Map<String,String> map){
        //判断是否有admin角色
        return login(map);
    }

    /**
     * 用户登录函数
     * @param map
     * @return
     */
    private DataResult login(Map<String,String> map){
        User userLogin = userService.getOneByAccount(map.get("userAccount"));
        try {
            //判断用户是否已经被锁定
            if (!userLogin.isDelete()) {
                return DataResult.failWithMsg("该用户已被锁定");
            }
            //验证用户密码是否正确
            if (userLogin.getUserPwd().equals(Md5Utils.encode(map.get("userPassword")))){
                //密码相同,返回token
                String token = jwtUtils.createJwt(String.valueOf(userLogin.getUserId()),userLogin.getUserAccount());
                return DataResult.success(token);
            } else {
                return DataResult.fail();
            }
        } catch (NullPointerException e){
            return DataResult.failWithMsg("invalid user");
        }
    }

    /**
     *     @PostMapping("/login")
     *     public DataResult login(@RequestBody User user){
     *         User userLogin = userService.getOneByAccount(user.getUserAccount());
     *         if (userLogin.getUserId()!=null){
     *             Map<String,Object> map= new HashMap<String, Object>();
     *             String token = jwtUtils.createJwt(userLogin.getUserAccount(),userLogin.getUserName(),map);
     *             return DataResult.success(token);
     *         }else {
     *             return DataResult.failWithMsg("用户不存在");
     *         }
     * @param map
     * @return
     */

    @PostMapping("/main")
    public DataResult main(@RequestParam Map<String,String> map){
        System.out.println(map.get("userPassword"));
        System.out.println("success");
        return DataResult.success();
    }
    @GetMapping("/getAllIdxcarousel")
    public DataResult getAllIdxcarousel() {
        List<IdxCarousel> idxCarouselList =  idxCarouselService.getAll();
        return DataResult.success(idxCarouselList);
    }
}
